package com.sensePlusVR.db;

import java.io.File;
import java.util.List;

import org.xutils.DbManager;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import android.content.Context;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.EntityBase;
import com.sensePlusVR.utils.FileUtils;
import com.sensePlusVR.utils.MyLog;

/**
 * http://bMyLog.csdn.net/tybaoerge/article/details/50414471
 * @author: Loren
 * @create: 2016-8-8下午3:20:24
 * @desc: 图片缓存建立数据库映射实体，亚马逊对应图片下载缓存机制
 *
 */
@Table(name = "ImageHistory"/*,onCreated = "CREATE UNIQUE INDEX unique_name ON ImageHistory(image_url)"*/)
public class ImageHistory extends EntityBase {
	private static final String TAG = ImageHistory.class.getName();	
	@Column(name = "image_url",property = "UNIQUE")
    public String image_url; //图片url 
	@Column(name = "file_name")
    public String file_name;
	@Column(name = "file_size")
    public long file_size;
	@Column(name = "file_time")
    public long file_time;
	
	public String getImage_url() {
		return image_url;
	}
	public void setImage_url(String image_url) {
		this.image_url = image_url;
	}
	public String getFile_name() {
		return file_name;
	}	
	public void setFile_name(String file_name) {
		this.file_name = file_name;
	}
	public long getFile_size() {
		return file_size;
	}
	public void setFile_size(long file_size) {
		this.file_size = file_size;
	}
	public long getFile_time() {
		return file_time;
	}
	public void setFile_time(long file_time) {
		this.file_time = file_time;
	}
	
	public static void update(ImageHistory h) {
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
    			db.saveOrUpdate(h);
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "ImageHistory update failed: "+e);
    	}
	}
	
	public static ImageHistory getImage(String image_url) {		
    	try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
    			DbManager db = VRApplication.getDbManager();
	    		return db.selector(ImageHistory.class).where("image_url","=",image_url).findFirst();
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "getImage failed: "+e);
    	}
    	return null;
	}
	
	public static void removeCacheFiles(int count) {
		try {
    		Context context = VRApplication.getAppContext();
    		if(context != null) {
	    		DbManager db = VRApplication.getDbManager();
	    		List<ImageHistory> list = db.selector(ImageHistory.class)
	    				.orderBy("file_time", true)
	    				.limit(count)
	    				.offset(0).findAll();
	    		
	    		if(list != null){
	    			for(ImageHistory h: list){
	    				try {
		    				File f = new File(getImageCacheDir()+"/"+h.getFile_name());
		    				if(f.exists()){
		    					f.delete();
		    				}	 
	    				}catch(Exception e){
	    				}
	    			}
	    			db.delete(list);
	    		}
    		}
    	}catch(Exception e){
    		MyLog.e(TAG, "getImage failed: "+e);
    	}
	}
	
	public static String getImageCacheDir() {
		return FileUtils.getExternalCacheDir(VRApplication.getAppContext()) + "/images";
	}
	public static long getImageCacheSize() {
		long isze = -1;
		try {
			Context context = VRApplication.getAppContext();
			if(context != null) {
				DbManager db = VRApplication.getDbManager();
				List<ImageHistory> list = db.selector(ImageHistory.class).findAll();
	    		if(list != null){
	    			long size = 0;
	    			for(ImageHistory h: list){
	    				size += h.getFile_size();
	    			}
	    			if(size != 0) isze = size;
	    		}
			}
		}catch(Exception e){
    		MyLog.e(TAG, "getImage failed: "+e);
    	}
		return isze;    	
    }
	@Override
	public String toString() {
		return "ImageHistory [image_url=" + image_url + ", file_name="
				+ file_name + ", file_size=" + file_size + ", file_time="
				+ file_time + "]";
	}
	
}
